package com.hornsun.medicalRecord.API;

import com.hornsun.domain.dbo.MedicalRecord;
import com.hornsun.domain.dbo.Prescription;
import com.hornsun.medicalRecord.dto.RecordInfoDTO;
import com.hornsun.prescription.service.PrescriptionService;
import com.hornsun.repository.MedicalRecordRepository;
import com.hornsun.repository.PrescriptionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 编译中... on 2018/1/29.
 */
@RestController
@RequestMapping("/api/record")
public class MedicalRecordAPI {
    @Autowired
    private MedicalRecordRepository medicalRecordRepository;
    @Autowired
    private PrescriptionService prescriptionService;

    /**
     * 保存就诊记录
     * @param record
     * @param prescription
     * @return
     */
    @PostMapping("/addRecord")
    public ResponseEntity<?> addRecord(@RequestParam("record") MedicalRecord record , @RequestParam("prescription")Prescription prescription){
        record.setPrescriptionGuid(prescription.getPrescriptionGuid());
        medicalRecordRepository.insertMedicalRecord(record);
        prescriptionService.addPrescription(prescription);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    /**
     * 查找最近一次就诊记录
     * @param guid
     * @return
     */
    @GetMapping("/getLastRecord")
    public ResponseEntity<RecordInfoDTO> getLastRecord(@RequestParam("guid")String guid){
        MedicalRecord medicalRecord = medicalRecordRepository.getLastRecord(guid);
        Prescription prescription =prescriptionService.queryPrescription(medicalRecord.getPrescriptionGuid());
        RecordInfoDTO recordInfoDTO = new RecordInfoDTO();
        recordInfoDTO.setMedicalRecord(medicalRecord);
        recordInfoDTO.setPrescription(prescription);
        return new ResponseEntity<>(recordInfoDTO,HttpStatus.OK);
    }

}
